﻿using FastCloud.Base;
using FastCloud.Base.Desgin;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/userrole")]
    public class UserRoleController : BaseController
    {
        [AutoWired]
        private DB_Role DB_Role;
        [AutoWired]
        private DB_User DB_User;
        [AutoWired]
        private DB_UserRole DB_UserRole;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/UserRole/UserRole_List.cshtml");
        }

        [Route("edit")]
        public IActionResult Add()
        {
            return View("~/Views/ORG/UserRole/UserRole_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string UserName = ValueConvert.ConvertToString(GetFormParam("UserName"));
            string OUGuid = ValueConvert.ConvertToString(GetFormParam("OUGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(UserName))
            {
                sql += " and UserName like @UserName";
                ParamList.Add(new ParamEntity() { ParamName = "UserName", ParamValue = "%" + UserName + "%" });
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(OUGuid) && OUGuid != "Top")
                {
                    sql += " and OUGuid=@OUGuid";
                    ParamList.Add(new ParamEntity() { ParamName = "OUGuid", ParamValue = "%" + OUGuid + "%" });
                }
            }
            List<Frame_UserRole> UserRoleList = DB_UserRole.SelectAll();
            List<Frame_User> list = DB_User.SelectPageList("*", sql, "SortNum desc", ParamList);
            foreach (Frame_User user in list)
            {
                List<Frame_UserRole> UserRoleFilterList = UserRoleList.Where(a => a.UserGuid == user.UserGuid).ToList();
                var RoleListStr = string.Empty;
                foreach (Frame_UserRole item in UserRoleFilterList)
                {
                    RoleListStr += item.RoleGuid + ";";
                }
                RoleListStr = RoleListStr.TrimEnd(';');
                user.RoleGuidList = RoleListStr;
            }
            int count = DB_User.SelectCount(sql);
            Page page = new Page();
            page.Rows = list;
            page.Total = count;
            return Result.OK(page);
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string UserGuid = ValueConvert.ConvertToString(GetFormParam("UserGuid"));
            Frame_User user = DB_User.SelectOne(UserGuid);
            List<Frame_UserRole> UserRoleList = DB_UserRole.SelectAll();
            List<Frame_UserRole> UserRoleFilterList = UserRoleList.Where(a => a.UserGuid == user.UserGuid).ToList();
            var RoleListStr = string.Empty;
            foreach (Frame_UserRole item in UserRoleFilterList)
            {
                RoleListStr += item.RoleGuid + ";";
            }
            RoleListStr = RoleListStr.TrimEnd(';');
            user.RoleGuidList = RoleListStr;
            return Result.OK(user);
        }


        [Route("batchUpdateUserRole"), HttpPost]
        public IActionResult BatchUpdateUserRole()
        {
            string UserRoleRelationStr = ValueConvert.ConvertToString(GetFormParam("UserRoleRelationStr"));
            JArray UserRoleRelationArray = (JArray)JsonConvert.DeserializeObject(UserRoleRelationStr);
            foreach (JObject item in UserRoleRelationArray)
            {
                string UserGuid = ValueConvert.ConvertToString(item.GetValue("UserGuid"));
                string RoleGuid = ValueConvert.ConvertToString(item.GetValue("RoleGuid"));
                bool IsCheck = ValueConvert.ConvertToBoolean(item.GetValue("IsCheck"));
                DB_UserRole.UpdateUserRole(UserGuid, RoleGuid, IsCheck);
            }
            return Result.OK();
        }



    }
}
